package com.micai.springboot.controller.activiti.controller;

import com.micai.springboot.service.UserService;
import com.micai.springboot.util.MessageUtils;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/10/17 19:08
 * @Description: 流程任务管理
 */
@Controller
@RequestMapping("/admin/tasks")
public class TaskController extends AbstractActivitiController {

    @Autowired
    private UserService userService;

    /**
     * 查询待办任务列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(org.springframework.ui.Model model) {
        // 读取直接分配给当前人或者已经签收的任务
        TaskQuery taskQuery = taskService.createTaskQuery().taskCandidateOrAssigned(getId().toString());
        List<Task> taskList = taskQuery.list();
        model.addAttribute("taskList", taskList);
        return "admin/workflow/taskList";
    }

    /**
     * 办理个人任务
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/complete/{taskId}")
    @ResponseBody
    public Map<String, Object> complete(@PathVariable("taskId") String taskId) {
        Map<String, Object> retMap = null;
        try {
            // 办理个人任务
            taskService.complete(taskId);
            retMap = MessageUtils.success("办理成功");
        } catch (Exception e) {
            LOGGER.error("办理个人任务失败：taskId={}", taskId, e);
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 查看我的组任务
     * @return
     */
    @RequestMapping(value = "/group")
    public String group(org.springframework.ui.Model model) {
        // 创建任务查询对象
        TaskQuery taskQuery = taskService.createTaskQuery().taskCandidateUser(getId().toString());
        List<Task> taskList = taskQuery.list();
        model.addAttribute("taskList", taskList);
        return "admin/workflow/groupTaskList";
    }

    /**
     * 根据任务id查询对应的流程变量数据
     * @return
     */
    @RequestMapping(value = "/findProcessVariables/{taskId}")
    @ResponseBody
    public Map<String, Object> findProcessVariables(@PathVariable("taskId") String taskId) {
        Map<String, Object> retMap = null;
        try {
            Map<String, Object> variables = taskService.getVariables(taskId);
            retMap = MessageUtils.success();
            // 返回流程变量数据
            retMap.put("variables", variables);
        } catch (Exception e) {
            LOGGER.error("根据任务id查询对应的流程变量数据失败：taskId={}", taskId, e);
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 根据任务id拾取组任务
     * @return
     */
    @RequestMapping(value = "/takeTask/{taskId}")
    @ResponseBody
    public Map<String, Object> takeTask(@PathVariable("taskId") String taskId) {
        Map<String, Object> retMap = null;
        try {
            String userId = getId().toString(); // 获取当前登录用户的id
            taskService.claim(taskId, userId);
            retMap = MessageUtils.success();
        } catch (Exception e) {
            LOGGER.error("根据任务id拾取组任务失败：taskId={}", taskId, e);
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 获取获取某用户所有历史流程实例集合，包括已完结和正在处理的
     * @param model
     * @return
     */
    @RequestMapping(value = "/getHisTask")
    public String getHisTask(org.springframework.ui.Model model) {
        //获取当前用户所有历史流程实例集合
        List<HistoricProcessInstance> historicProcessInstanceList = historyService.createHistoricProcessInstanceQuery().startedBy(getId().toString()).list();
        model.addAttribute("historicProcessInstanceList", historicProcessInstanceList);
        return "admin/workflow/hisTaskList";
    }
}
